package com.example.searchcenter.center;

import com.example.searchcenter.R;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesUtil;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

/***
 * 
 * @author NguyenTrung
 * Function: Load google map and view places
 */
public class MapsFragment extends Fragment 
implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener
{
	// variable for google map
	GoogleMap map;
	MapView mapView;
	// variable for place
	Double latitude, longitude;
	String place_lat, place_long;
	public static LatLng myLatLng = null;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_maps,
				container, false);
		
		try {
		    MapsInitializer.initialize(getActivity());
		} catch (GooglePlayServicesNotAvailableException e) {
		    Log.e("Address Map", "Could not initialize google play", e);
		}

		switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()) )
		{
			case ConnectionResult.SUCCESS:
			  mapView = (MapView) rootView.findViewById(R.id.map);
			  mapView.onCreate(savedInstanceState);
			  // Gets to GoogleMap from the MapView and does initialization stuff
			  if(mapView!=null)
			  {
				  place_lat = DetailsCenter.place_lat;
				  place_long = DetailsCenter.place_long;
				  latitude = Double.parseDouble(place_lat);
				  longitude = Double.parseDouble(place_long);
				  map = mapView.getMap();
				  map.getUiSettings().setMyLocationButtonEnabled(false);
				  map.setMyLocationEnabled(true);
				  
				  // Create marker
				  MarkerOptions marker = new MarkerOptions().position(
							new LatLng(latitude, longitude)).title(DetailsCenter.name_center);
				  marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
				  
				  // Add marker
				  Marker location = map.addMarker(marker);
				  location.showInfoWindow();
				  CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 15);
				  map.animateCamera(cameraUpdate);
			  }
			  break;
			case ConnectionResult.SERVICE_MISSING: 
			  Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show();
			  break;
			case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED: 
			  Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show();
			  break;
			default: Toast.makeText(getActivity(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()), Toast.LENGTH_SHORT).show();
		}
		return rootView;
	}
	
	@Override
	public void onResume() {
	mapView.onResume();
	super.onResume();
	}
	@Override
	public void onDestroy() {
	super.onDestroy();
	mapView.onDestroy();
	}
	@Override
	public void onLowMemory() {
	super.onLowMemory();
	mapView.onLowMemory();
	}

	@Override
	public void onConnectionFailed(ConnectionResult arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onConnected(Bundle arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDisconnected() {
		// TODO Auto-generated method stub
		
	}
}
